#include "EItem.h"
#include "EItemManager.h"
#include "EItemVisitor.h"
#include "EPlugin.h"
#include "EVariant.h"
#include <iostream>
#include <vector>
#include <string>
#include <DefineCommon.h>
#include <EPluginLoader.h>
// #include <EPluginDefault.h>

int main_test_EPluginLoader(int argc, const char *argv[])//_test_EPluginLoader
{
    ELOG_INFO("ExeCommon test EPluginLoader");

    /**test EItem tree adding
    EPlugin::HandleDll handle = nullptr;
    EPlugin::Fun_Address fun = nullptr;
    EPluginLoader::instance()->loadLibrary("D:\\eric\\coding\\EEditor2\\source\\build_vs2019_x64\\bin\\plugin\\PluginDefaultd.dll"
            , handle
            , fun);
    */

    /*Test loadPlugin function, using PluginDefault*/
    EPluginLoader::instance()->loadPlugin("PluginDefaultd");

    EItemVisitorPtr visitor = NewSmartPtr(EItemVisitor)();
    visitor->travelEItem(EItemManager::instance()->getItemTypeRoot());

    EItemPtr typeParent = EItemManager::instance()->getItemTypeRoot()->getChild(0)->getChild(0);
    EItemPtr typeChild = EItemManager::instance()->getItemTypeRoot()->getChild(0)->getChild(1);

    //new a parent object
    {
        EString funName = typeParent->getName() + "_Fun_factory";
        EItemMetaFunctionPtr fun = typeParent->getOrCreatMeta()->getFunction(funName);
        if(fun != nullptr)
        {
            ELOG_DEBUG("funName=%s", fun->getName().data());
            EVariant result;
            fun->call(&result);
            EItem* newItemRawPtr = (EItem*)result.to_intptr_t();
            EItemPtr newItemPtr(newItemRawPtr);
            EString metaInfo = newItemPtr->getOrCreatMeta()->getPrintStr();
            ELOG_INFO("EItemInPluginParent's metaInfo=%s", metaInfo.data());
        }
    }

    //new a child object
    {
        EString funName = typeChild->getName() + "_Fun_factory";
        EItemMetaFunctionPtr fun = typeChild->getOrCreatMeta()->getFunction(funName);
        if(fun != nullptr)
        {
            ELOG_DEBUG("funName=%s", fun->getName().data());
            EVariant result;
            fun->call(&result);
            EItem* newItemRawPtr = (EItem*)result.to_intptr_t();
            EItemPtr newItemPtr(newItemRawPtr);
            EString metaInfo = newItemPtr->getOrCreatMeta()->getPrintStr();
            ELOG_INFO("EItemInPlugin's metaInfo=%s", metaInfo.data());
        }
    }

    return 1;

}
